package com.elbis.business.manager;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import com.elbis.business.entity.locale.City;
import com.elbis.business.entity.locale.Country;
import com.elbis.business.entity.locale.Language;
import com.elbis.util.HibernateUtil;

public class LocaleManager {

	private static final Logger logger = Logger.getLogger(LocaleManager.class);

	private LocaleManager() {
	}

	public static Long saveNewLanguage(Language language) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		Long id = null;
		try {
			trx = session.beginTransaction();
			id = (Long) session.save(language);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return id;
	}

	public static Language getLanguage(String code) {
		Session session = HibernateUtil.getSession();
		Language language = null;
		try {
			language = (Language) session.createCriteria(Language.class)
					.add(Restrictions.and(Restrictions.eq("code", code)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return language;
	}

	public static Language getLanguageByName(String name) {
		Session session = HibernateUtil.getSession();
		Language language = null;
		try {
			language = (Language) session.createCriteria(Language.class)
					.add(Restrictions.and(Restrictions.eq("name", name)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return language;
	}

	@SuppressWarnings("unchecked")
	public static List<Language> getLanguages() {
		Session session = HibernateUtil.getSession();
		List<Language> langs = null;
		try {
			langs = (List<Language>) session.createCriteria(Language.class)
					.list();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return langs;
	}

	public static City getCity(Country country, String name) {
		Session session = HibernateUtil.getSession();
		City city = null;
		try {
			city = (City) session
					.createCriteria(City.class)
					.add(Restrictions.and(Restrictions.eq("name", name),
							Restrictions.eq("country", country)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return city;
	}

	public static City getCityByName(String name) {
		Session session = HibernateUtil.getSession();
		City city = null;
		try {
			city = (City) session.createCriteria(City.class)
					.add(Restrictions.and(Restrictions.eq("name", name)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return city;
	}

	public static Long saveNewCity(City city) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		Long id = null;
		try {
			trx = session.beginTransaction();
			id = (Long) session.save(city);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return id;
	}

	public static Long saveNewCountry(Country country) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		Long id = null;
		try {
			trx = session.beginTransaction();
			id = (Long) session.save(country);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return id;
	}

	public static void updateCountry(Country country) {
		Session session = HibernateUtil.getSession();
		Transaction trx = null;
		try {
			trx = session.beginTransaction();
			session.update(country);
			trx.commit();
		} catch (HibernateException e) {
			if (trx != null) {
				trx.rollback();
			}
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
	}

	public static Country getCountry(String code) {
		Session session = HibernateUtil.getSession();
		Country country = null;
		try {
			country = (Country) session.createCriteria(Country.class)
					.add(Restrictions.and(Restrictions.eq("code", code)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return country;
	}

	public static Country getCountryByName(String name) {
		Session session = HibernateUtil.getSession();
		Country country = null;
		try {
			country = (Country) session.createCriteria(Country.class)
					.add(Restrictions.and(Restrictions.eq("name", name)))
					.uniqueResult();

		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return country;
	}

	@SuppressWarnings("unchecked")
	public static List<Country> getCountryList() {
		Session session = HibernateUtil.getSession();
		List<Country> countries = null;
		try {
			countries = (List<Country>) session.createCriteria(Country.class)
					.list();
		} catch (HibernateException e) {
			logger.error(e.getMessage());
		} finally {
			session.close();
		}
		return countries;
	}

}
